package com.sesame.api;

import kim.sesame.framework.utils.GData;
import kim.sesame.framework.utils.Ipconfig;
import lombok.Data;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Data
@CommonsLog
@Component
@ConfigurationProperties(prefix = "fms")
public class FMSConfig implements InitializingBean {

    private String filePath;
    private String mapping;


    @Resource
    private Environment environment;

    @Override
    public void afterPropertiesSet() throws Exception {
        if (StringUtils.isEmpty(this.mapping)) {
            StringBuffer sb = new StringBuffer("http://" + Ipconfig.getInfo().getLocalip());

            String port = environment.getProperty("server.port");

            if (StringUtils.isEmpty(port)) {
                sb.append(":8080");
            } else if (port.equals("80")) {

            } else {
                sb.append(":" + port);
            }
            String basePath = environment.getProperty(GData.SPRINGBOOT.contextPath);
            if(StringUtils.isNotEmpty(basePath)){
                sb.append(basePath);
            }
            this.mapping = sb.toString();
        }
    }
}
